﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using com.dz.ht.components;
using System.Web;
using com.dz.ht.common;
using System.Text.RegularExpressions;
namespace com.dz.ht.ui
{
    public partial class FrmUpdateSitiList : Form
    {
        string m_book_id = string.Empty;
        string m_book_name = string.Empty;
        public FrmUpdateSitiList()
        {
            InitializeComponent();
        }

        public FrmUpdateSitiList(string bookId,string bookName)
        {
            m_book_id = bookId;
            m_book_name = bookName;
            InitializeComponent();
        }

        private void FrmUpdateSitiList_Load(object sender, EventArgs e)
        {
            label1.Text = string.Format("BookId：{0}，BookName：{1}", m_book_id, m_book_name);

            BindUrl();
        }

        private void BindUrl()
        {
            DataTable dt = new DataTable();
            DataColumn t_colume_url = new DataColumn("url", typeof(string));
            DataColumn t_colume_name = new DataColumn("name", typeof(string));
            dt.Columns.Add(t_colume_url);
            dt.Columns.Add(t_colume_name);

            /// <summary>
            /// 百度首页编码
            /// </summary>
            Encoding t_encoding = Encoding.GetEncoding("gb2312");
            string i_chapter_content_url = string.Format("http://www.baidu.com/s?wd={0}", HttpUtility.UrlEncode(string.Format("{0} 最新章节",m_book_name), t_encoding));

              string t_html = NetSiteCatchManager.ReadUrl(i_chapter_content_url, t_encoding);

              if (!string.IsNullOrEmpty(t_html))
              {
                  MatchCollection t_ma = Regex.Matches(t_html, "<h3\\s*?class=[\'\"]?t[\'\"]?><a[^<>]*?hrefs*=s*[\'\"]*([^\"\']*)[\'\"]*[^<>]*?>(.*?)</a>\\s*?</h3>", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                  if (t_ma == null || t_ma.Count <= 0)
                      return;

                  List<string> t_list_url = new List<string>();
                  foreach (Match t_mc in t_ma)
                  {
                      DataRow t_row = dt.NewRow();
                      t_row[0] = t_mc.Groups[1].Value.ToString();
                      t_row[1] = t_mc.Groups[2].Value.ToString();
                      dt.Rows.Add(t_row);
                  }

                  dataGridView1.DataSource = dt;
              }
        }

        private void 打开网址ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count <= 0)
                return;
            string t_url = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();

            System.Diagnostics.Process.Start(t_url);
        }

        private void 抽取章节ToolStripMenuItem_Click(object sender, EventArgs e)
        {
             if (dataGridView1.SelectedRows.Count <= 0)
                return;
            string t_url = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            FrmRegChapterList t_frm = new FrmRegChapterList(m_book_id, m_book_name, t_url);
            t_frm.ShowDialog();

        }

        private void 从库存设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count <= 0)
                return;
            string t_url = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            Uri t_domain_url = new Uri(t_url);

            SearchSiteInfo t_site = new SearchSiteInfo();
            long t_book_id = 0;
            if (!long.TryParse(m_book_id, out t_book_id))
                t_book_id = 0;
            t_site.BookId = t_book_id;
            t_site.ChapterUrl = t_url;
            t_site.SiteDomain = "http://" + t_domain_url.Host;
           
            try
            {
                DataManger db = new DataManger();
                int ret = db.UpdateBookSearchSite(t_site);
                if (ret == Consts.Success)
                {
                    MessageBox.Show("保存成功");
                }
                else
                {
                    MessageBox.Show("库存没有");
                }

            }
            catch (Exception ex)
            {
                LogHelper.Error("保存搜索站点失败" + ex.ToString());
                MessageBox.Show("保存失败");
            }
        }
    }
}
